#include "configmanager.h"

ConfigManager* ConfigManager::instance_p = nullptr;

ConfigManager::ConfigManager(QString work_path)
{
    this->work_path = work_path;
    if(!QFile::exists(this->getConfigPath())){
        initConfigFile();
    }else {
        readConfigFile();
    }

}

/**
 * @brief ConfigManager::instance 单例模式，获取ConfigManager对象
 * @param work_path 工作路径
 * @return ConfigManager对象
 */
ConfigManager* ConfigManager::instance(QString work_path) {
    if(!instance_p) {
        instance_p = new ConfigManager(work_path);
    }
    return instance_p;
}

ConfigManager* ConfigManager::instance(){
    return instance_p;
}

/**
 * @brief ConfigManager::getConfigPath 获取配置文件位置
 * @return 配置文件位置
 */
QString ConfigManager::getConfigPath(){
    return this->work_path + "/config.json";
}

/**
 * @brief ConfigManager::initConfigFile 初始化配置文件
 */
void ConfigManager::initConfigFile(){
    QString filePath = this->getConfigPath();
    QFile fileOut(filePath);
    if(!fileOut.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QMessageBox::critical(nullptr,"初始化配置文件出错","无法写入配置文件，请检查是否拥有写入权限！",QMessageBox::Ok);
        return;
    }

    QFile fileIn(":/default/resources/default_config.json");
    if(!fileIn.exists()){
        QMessageBox::critical(nullptr,"初始化配置文件出错","资源文件损坏！请自行写入“config.json”文件！",QMessageBox::Ok);
        fileOut.close();
        return;
    }

    if(!fileIn.open(QIODevice::ReadOnly | QIODevice::Text)){
        QMessageBox::critical(nullptr,"初始化配置文件出错","资源文件无法读取！请自行写入“config.json”文件！",QMessageBox::Ok);
        fileOut.close();
        return;
    }
    QByteArray jsonData = fileIn.readAll();
    fileOut.write(jsonData);
    fileOut.close();
    fileIn.close();

    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
    if(!jsonDoc.isNull()) {
        this->deviceData = jsonDoc.object();
    }
}


/**
 * @brief ConfigManager::readConfigFile 读取配置文件
 */
void ConfigManager::readConfigFile(){
    QFile fileIn(this->getConfigPath());
    if(!fileIn.open(QIODevice::ReadOnly | QIODevice::Text)){
        QMessageBox::critical(nullptr,"初始化配置文件出错","无法读取配置文件，请检查是否存在或有权读写！",QMessageBox::Ok);
        return;
    }
    QByteArray jsonData = fileIn.readAll();
    QJsonParseError jsonError;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &jsonError);
    if(jsonDoc.isNull()){
        QMessageBox::critical(nullptr,
                              "初始化配置文件出错",
                              QString("解析配置文件出错: %1").arg(jsonError.errorString()),
                              QMessageBox::Ok);
        return;
    }
    this->deviceData = jsonDoc.object();
    fileIn.close();
}

/**
 * @brief ConfigManager::getDeviceType 设备名称获取设备型号
 * @param deviceName 设备名称
 * @return 匹配到的设备型号 ConfigManager::DeviceType
 */
int ConfigManager::getDeviceType(QString deviceName){
    for(const QString& key : this->deviceData.keys()){
        QJsonArray array = this->deviceData[key].toArray();
        for(const QJsonValue& value : array) {
            if(deviceName.contains(value.toString(),Qt::CaseInsensitive)){
                if (key == "iosType") return this->IOS;
                if (key == "androidType") return this->ANDROID;
                if (key == "hostType") return this->HOST;
            }
        }
    }
    return this->OTHER;
}

QString ConfigManager::getDeviceName(QString device){
    for(const QString& key : this->deviceData.keys()) {
        QJsonArray array = this->deviceData[key].toArray();
        for(const QJsonValue& value : array) {
            if(device.contains(value.toString(), Qt::CaseInsensitive)){
                return value.toString();
            }
        }
    }
    return "Unknown";
}

QString ConfigManager::toString(){
    return QJsonDocument(this->deviceData).toJson(QJsonDocument::Indented);
}
